/*
	This file is part of "Ants on the web".

    "Ants on the web" is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Ants on the web" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with "Ants on the web".  If not, see <http://www.gnu.org/licenses/>.
*/

package org.utc.ia04.antsontheweb.pheromone;

import java.net.MalformedURLException;
import java.net.URL;

import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;


public class PheromoneManagerAgent extends Agent
{
	//serialVersionUID
	private static final long serialVersionUID = 1L;
	
	//DF Information
	public static final String dfType = "PheromoneManagerServices";
	public static final String dfName = "PheromoneManager";
	
	//Errors
	public static final String UNFORMATTED = "Unformatted message...";
	
	//Attributes
	private NetGraphic graphic;
	
	//Setup
	protected void setup()
	{
		/* --- DEBUG ---
		System.out.println("PheromoneManager cree !");
		//-------------*/
		
		Object[] args = getArguments();
		try {
			graphic = new NetGraphic( new URL(args[0].toString()));
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		addBehaviour(new PheromoneManagerReceiveBhv());
		register();
	}

	//Accessors
	public NetGraphic getGraphic() { return graphic; }
	public void setGraphic(NetGraphic g) { this.graphic = g; }
	
	private void register()
	{
		DFAgentDescription dfd = new DFAgentDescription();
		dfd.setName(getAID());
		ServiceDescription sd = new ServiceDescription();
		sd.setType(PheromoneManagerAgent.dfType); sd.setName(PheromoneManagerAgent.dfName);
		dfd.addServices(sd);
		try { DFService.register(this, dfd); }
		catch (Exception fe) { System.out.println("Error during registration..."); }
	}
	
	public void takeDown()
	{
		DFAgentDescription dfd = new DFAgentDescription();
		dfd.setName(getAID());

		try { DFService.deregister(this, dfd); }
		catch (FIPAException e) { }
	}
}
